<?php

namespace App\Http\Controllers\Admin\shop;


use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Shop\ProductCategory as Category;

class CategoryController extends Controller
{
    public function __construct()
    {
        view()->share([
            'categories' => Category::get_categories(),
        ]);
    }
    function index()
    {
        return view('admin.shop.categories.index');
    }

    public function create()
    {
        return view('admin.shop.categories.create');
    }

    public function store(Request $request)
    {
        Category::create($request->all());
        Category::clear();
        return redirect(route('admin.category.index'))->with('success', '添加栏目成功');
    }

    public function edit($id)
    {
        $category = Category::find($id);
        return view('admin.Shop.categories.edit')->with('category', $category);
    }

    function update(Request $request, $id)
    {
        $category = Category::find($id);
        $category->update($request->all());
        Category::clear();
        return redirect(route('admin.category.index'))->with('success', '编辑成功~');
    }

    function is_something(Request $request)
    {
        $attr = $request->attr;
        $category = Category::find($request->id);
        $value = $category->$attr ? false : true;
        $category->$attr = $value;
        $category->save();
        Category::clear();
    }

    function sort_order(Request $request)
    {
        $category = Category::find($request->id);
        $category->sort_order = $request->sort_order;
        $category->save();
        Category::clear();
    }

    function destroy($id)
    {
        if (!Category::check_children($id)) {
            return back()->with('error', '当前分类有子分类，请先将子分类删除后再尝试删除~');
        }

        if (!Category::check_products($id)) {
            return back()->with('error', '当前分类有商品，请先将对应商品删除后再尝试删除~');
        }

        Category::destroy($id);
        Category::clear();
        return back()->with('success', '删除成功');
    }
    /*
    function index()
    {
        $categories = Category::all();
        //return $categories;
        return view('admin.shop.categories.index')->with('categories',$categories);
    }
    public function create()
    {
        $categories = Category::where('parent_id', '0')->get();;
        //return $categories;
        return view('admin.shop.categories.create')->with('categories',$categories);
    }

    function store(Request $request)
    {
        Category::create($request->all());
        return redirect(route('admin.category.index'))->with('success', '添加成功');
    }
    function edit($id)
    {
        $categories = Category::where('parent_id', '0')->get();;
        $category = Category::find($id);
        //return $categories;
        return view ('admin.shop.categories.edit')->with('category',$category)->with('categories',$categories);
    }
    function update(Request $request,$id)
    {
        $category = Category::find($id);
        $category->update($request->all());
        return redirect(route('admin.category.index'))->with('success', '编辑品牌成功~');
    }
    */
}
